package com.kakarote.finance.common;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kakarote.common.entity.UserInfo;
import com.kakarote.core.redis.Redis;
import com.kakarote.core.servlet.ApplicationContextHolder;
import com.kakarote.core.utils.UserUtil;
import com.kakarote.finance.entity.PO.FinanceAccountSet;
import com.kakarote.finance.entity.PO.FinanceAccountUser;
import com.kakarote.finance.service.IFinanceAccountSetService;
import com.kakarote.finance.service.IFinanceAccountUserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class AccountSet {

    private static IFinanceAccountSetService setService = ApplicationContextHolder.getBean(IFinanceAccountSetService.class);

    private static IFinanceAccountUserService accountUserService = ApplicationContextHolder.getBean(IFinanceAccountUserService.class);

    private static final ThreadLocal<FinanceAccountSet> accountSetThreadLocal = new ThreadLocal<>();

    private static Redis redis = ApplicationContextHolder.getBean(Redis.class);
    ;

    public static FinanceAccountSet getAccountSet() {
        UserInfo user = UserUtil.getUser();
        FinanceAccountSet redisObject = redis.get(user.getUserId() + "");
        return redisObject;
    }

    public static Long getAccountSetId() {
        //TODO 默认账号ID
//    	return 1804084920850509824L;
        UserInfo user = UserUtil.getUser();
        FinanceAccountSet financeAccountSet = redis.get(user.getUserId() + "");
        if (financeAccountSet != null) {
            return financeAccountSet.getAccountId();
        } else {
            init();
        }
        return -1L;
    }


    public static void setAccountSet(FinanceAccountSet accountSet) {
        UserInfo user = UserUtil.getUser();
        redis.set(user.getUserId() + "", accountSet);
//        accountSetThreadLocal.set(accountSet);
    }

    public static void remove() {
        accountSetThreadLocal.remove();
    }

    public static  void init() {
        UserInfo user = com.kakarote.common.utils.UserUtil.getUser();
        //查看当前登录用户是否有默认账套
        FinanceAccountUser accountUser = new FinanceAccountUser();
        List<FinanceAccountUser> accountUserList = accountUserService.lambdaQuery().eq(FinanceAccountUser::getUserId, user.getUserId()).eq(FinanceAccountUser::getIsDefault, 1).isNotNull(FinanceAccountUser::getRoleId).list();
        if (CollUtil.isEmpty(accountUserList)) {
            //查询是否有全部账套
            List<FinanceAccountUser> userList = accountUserService.lambdaQuery().eq(FinanceAccountUser::getUserId, user.getUserId()).eq(FinanceAccountUser::getIsDefault, 0).isNotNull(FinanceAccountUser::getRoleId).list();
            if (userList.size() > 0) {
                //取第一个
                accountUser = userList.get(0);
                //并设置默认
                accountUserService.lambdaUpdate().eq(FinanceAccountUser::getId, accountUser.getId()).set(FinanceAccountUser::getIsDefault, 1).update();
            }
        } else if (accountUserList.size() > 1) {
            //默认取第一个
            accountUser = accountUserList.get(0);
            //并设置默认
            accountUserService.lambdaUpdate().ne(FinanceAccountUser::getId, accountUser.getId()).set(FinanceAccountUser::getIsDefault, 0).update();
        } else {
            //默认取第一个
            accountUser = accountUserList.get(0);
        }
        FinanceAccountSet accountSet = setService.getById(accountUser.getAccountId());
        if (accountSet == null) {
            accountSet = new FinanceAccountSet();
        }
        setAccountSet(accountSet);
    }
}
